home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / fluid / Fl_Type.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-10  |  15.6 KB  |  533 lines

  1. //
  2. // "$Id: Fl_Type.h,v 1.5.2.5 1999/09/10 16:40:17 bill Exp $"
  3. //
  4. // Widget type header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Each object described by Fluid is one of these objects.  They
  7. // are all stored in a double-linked list.
  8. //
  9. // There is also a single "factory" instance of each type of this.
  10. // The method "make()" is called on this factory to create a new
  11. // instance of this object.  It could also have a "copy()" function,
  12. // but it was easier to implement this by using the file read/write
  13. // that is needed to save the setup anyways.
  14. // Copyright 1998-1999 by Bill Spitzak and others.
  15. //
  16. // This library is free software; you can redistribute it and/or
  17. // modify it under the terms of the GNU Library General Public
  18. // License as published by the Free Software Foundation; either
  19. // version 2 of the License, or (at your option) any later version.
  20. //
  21. // This library is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24. // Library General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU Library General Public
  27. // License along with this library; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  29. // USA.
  30. //
  31. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  32. //
  33.  
  34. #include <FL/Fl_Widget.H>
  35. #include <FL/Fl_Menu.H>
  36. #include "Fluid_Image.h"
  37.  
  38. class Fl_Type {
  39.  
  40.   friend class Widget_Browser;
  41.   friend Fl_Widget *make_type_browser(int,int,int,int,const char *l=0);
  42.   friend class Fl_Window_Type;
  43.   virtual void setlabel(const char *); // virtual part of label(char*)
  44.  
  45. protected:
  46.  
  47.   Fl_Type();
  48.  
  49.   const char *name_;
  50.   const char *label_;
  51.   const char *callback_;
  52.   const char *user_data_;
  53.   const char *user_data_type_;
  54.  
  55. public:    // things that should not be public:
  56.  
  57.   Fl_Type *parent; // parent, which is previous in list
  58.   char new_selected; // browser highlight
  59.   char selected; // copied here by selection_changed()
  60.   char open_;    // state of triangle in browser
  61.   char visible; // true if all parents are open
  62.   char rtti;    // hack because I have no rtti, this is 0 for base class
  63.   int level;    // number of parents over this
  64.   static Fl_Type *first, *last; // linked list of all objects
  65.   Fl_Type *next, *prev;    // linked list of all objects
  66.  
  67.   Fl_Type *factory;
  68.   const char *callback_name();
  69.  
  70. public:
  71.  
  72.   virtual ~Fl_Type();
  73.   virtual Fl_Type *make() = 0;
  74.  
  75.   void add(Fl_Type *parent); // add as new child
  76.   void insert(Fl_Type *n); // insert into list before n
  77.   Fl_Type* remove();    // remove from list
  78.   void move_before(Fl_Type*); // move before a sibling
  79.  
  80.   virtual const char *title(); // string for browser
  81.   virtual const char *type_name() = 0; // type for code output
  82.  
  83.   const char *name() const {return name_;}
  84.   void name(const char *);
  85.   const char *label() const {return label_;}
  86.   void label(const char *);
  87.   const char *callback() const {return callback_;}
  88.   void callback(const char *);
  89.   const char *user_data() const {return user_data_;}
  90.   void user_data(const char *);
  91.   const char *user_data_type() const {return user_data_type_;}
  92.   void user_data_type(const char *);
  93.  
  94.   virtual Fl_Type* click_test(int,int);
  95.   virtual void add_child(Fl_Type*, Fl_Type* beforethis);
  96.   virtual void move_child(Fl_Type*, Fl_Type* beforethis);
  97.   virtual void remove_child(Fl_Type*);
  98.  
  99.   static Fl_Type *current;  // most recently picked object
  100.   virtual void open();    // what happens when you double-click
  101.  
  102.   // read and write data to a saved file:
  103.   void write();
  104.   virtual void write_properties();
  105.   virtual void read_property(const char *);
  106.   virtual int read_fdesign(const char*, const char*);
  107.  
  108.   // write code, these are called in order:
  109.   virtual void write_static(); // write static stuff to .c file
  110.   virtual void write_code1(); // code and .h before children
  111.   virtual void write_code2(); // code and .h after children
  112.  
  113.   // fake rtti:
  114.   virtual int is_parent() const;
  115.   virtual int is_widget() const;
  116.   virtual int is_button() const;
  117.   virtual int is_valuator() const;
  118.   virtual int is_menu_item() const;
  119.   virtual int is_menu_button() const;
  120.   virtual int is_group() const;
  121.   virtual int is_window() const;
  122.   virtual int is_code_block() const;
  123.   virtual int is_decl_block() const;
  124.   virtual int is_class() const;
  125.  
  126.   const char* class_name() const;
  127. };
  128.  
  129. class Fl_Function_Type : public Fl_Type {
  130.   const char* return_type;
  131.   char public_, cdecl_, constructor, havewidgets;
  132. public:
  133.   Fl_Type *make();
  134.   void write_code1();
  135.   void write_code2();
  136.   void open();
  137.   int ismain() {return name_ == 0;}
  138.   virtual const char *type_name() {return "Function";}
  139.   virtual const char *title() {
  140.     return name() ? name() : "main()";
  141.   }
  142.   int is_parent() const {return 1;}
  143.   int is_code_block() const {return 1;}
  144.   void write_properties();
  145.   void read_property(const char *);
  146. };
  147.  
  148. class Fl_Code_Type : public Fl_Type {
  149. public:
  150.   Fl_Type *make();
  151.   void write_code1();
  152.   void write_code2();
  153.   void open();
  154.   virtual const char *type_name() {return "code";}
  155.   int is_code_block() const {return 0;}
  156. };
  157.  
  158. class Fl_CodeBlock_Type : public Fl_Type {
  159.   const char* after;
  160. public:
  161.   Fl_Type *make();
  162.   void write_code1();
  163.   void write_code2();
  164.   void open();
  165.   virtual const char *type_name() {return "codeblock";}
  166.   int is_code_block() const {return 1;}
  167.   int is_parent() const {return 1;}
  168.   void write_properties();
  169.   void read_property(const char *);
  170. };
  171.  
  172. class Fl_Decl_Type : public Fl_Type {
  173.   char public_;
  174. public:
  175.   Fl_Type *make();
  176.   void write_code1();
  177.   void write_code2();
  178.   void open();
  179.   virtual const char *type_name() {return "decl";}
  180.   void write_properties();
  181.   void read_property(const char *);
  182. };
  183.  
  184. class Fl_DeclBlock_Type : public Fl_Type {
  185.   const char* after;
  186. public:
  187.   Fl_Type *make();
  188.   void write_code1();
  189.   void write_code2();
  190.   void open();
  191.   virtual const char *type_name() {return "declblock";}
  192.   void write_properties();
  193.   void read_property(const char *);
  194.   int is_parent() const {return 1;}
  195.   int is_decl_block() const {return 1;}
  196. };
  197.  
  198. class Fl_Class_Type : public Fl_Type {
  199.   const char* subclass_of;
  200.   char public_;
  201. public:
  202.   // state variables for output:
  203.   char write_public_state; // true when public: has been printed
  204.   Fl_Class_Type* parent_class; // save class if nested
  205. //
  206.   Fl_Type *make();
  207.   void write_code1();
  208.   void write_code2();
  209.   void open();
  210.   virtual const char *type_name() {return "class";}
  211.   int is_parent() const {return 1;}
  212.   int is_decl_block() const {return 1;}
  213.   int is_class() const {return 1;}
  214.   void write_properties();
  215.   void read_property(const char *);
  216. };
  217.  
  218. #define NUM_EXTRA_CODE 4
  219.  
  220. class Fl_Widget_Type : public Fl_Type {
  221.   virtual Fl_Widget *widget(int,int,int,int) = 0;
  222.   virtual Fl_Widget_Type *_make() = 0; // virtual constructor
  223.   virtual void setlabel(const char *);
  224.  
  225.   const char *extra_code_[NUM_EXTRA_CODE];
  226.   const char *subclass_;
  227.   uchar hotspot_;
  228.  
  229. protected:
  230.  
  231.   void write_static();
  232.   void write_code1();
  233.   void write_widget_code();
  234.   void write_extra_code();
  235.   void write_block_close();
  236.   void write_code2();
  237.  
  238. public:
  239.  
  240.   const char *xclass; // junk string, used for shortcut
  241.   Fl_Widget *o;
  242.   int public_;
  243.  
  244.   Fluid_Image *image;
  245.   void setimage(Fluid_Image *);
  246.  
  247.   Fl_Widget_Type();
  248.   Fl_Type *make();
  249.   void open();
  250.  
  251.   const char *extra_code(int n) const {return extra_code_[n];}
  252.   void extra_code(int n,const char *);
  253.   const char *subclass() const {return subclass_;}
  254.   void subclass(const char *);
  255.   uchar hotspot() const {return hotspot_;}
  256.   void hotspot(uchar v) {hotspot_ = v;}
  257.   uchar resizable() const;
  258.   void resizable(uchar v);
  259.  
  260.   virtual int textstuff(int what, Fl_Font &, int &, Fl_Color &);
  261.   virtual Fl_Menu_Item *subtypes();
  262.  
  263.   virtual int is_widget() const;
  264.  
  265.   virtual void write_properties();
  266.   virtual void read_property(const char *);
  267.   virtual int read_fdesign(const char*, const char*);
  268.  
  269.   ~Fl_Widget_Type();
  270.   void redraw();
  271. };
  272.  
  273. #include <FL/Fl_Tabs.H>
  274. #include <FL/Fl_Pack.H>
  275.  
  276. class igroup : public Fl_Group {
  277. public:
  278.   void resize(int,int,int,int);
  279.   igroup(int x,int y,int w,int h) : Fl_Group(x,y,w,h) {Fl_Group::current(0);}
  280. };
  281.  
  282. class itabs : public Fl_Tabs {
  283. public:
  284.   void resize(int,int,int,int);
  285.   itabs(int x,int y,int w,int h) : Fl_Tabs(x,y,w,h) {}
  286. };
  287.  
  288. class Fl_Group_Type : public Fl_Widget_Type {
  289. public:
  290.   virtual const char *type_name() {return "Fl_Group";}
  291.   Fl_Widget *widget(int x,int y,int w,int h) {
  292.     igroup *g = new igroup(x,y,w,h); Fl_Group::current(0); return g;}
  293.   Fl_Widget_Type *_make() {return new Fl_Group_Type();}
  294.   Fl_Type *make();
  295.   void write_code1();
  296.   void write_code2();
  297.   void add_child(Fl_Type*, Fl_Type*);
  298.   void move_child(Fl_Type*, Fl_Type*);
  299.   void remove_child(Fl_Type*);
  300.   int is_parent() const {return 1;}
  301.   int is_group() const {return 1;}
  302. };
  303.  
  304. extern const char pack_type_name[];
  305. extern Fl_Menu_Item pack_type_menu[];
  306.  
  307. class Fl_Pack_Type : public Fl_Group_Type {
  308.   Fl_Menu_Item *subtypes() {return pack_type_menu;}
  309. public:
  310.   virtual const char *type_name() {return pack_type_name;}
  311.   Fl_Widget_Type *_make() {return new Fl_Pack_Type();}
  312. };
  313.  
  314. extern const char tabs_type_name[];
  315.  
  316. class Fl_Tabs_Type : public Fl_Group_Type {
  317. public:
  318.   virtual const char *type_name() {return tabs_type_name;}
  319.   Fl_Widget *widget(int x,int y,int w,int h) {
  320.     itabs *g = new itabs(x,y,w,h); Fl_Group::current(0); return g;}
  321.   Fl_Widget_Type *_make() {return new Fl_Tabs_Type();}
  322.   Fl_Type* click_test(int,int);
  323.   void add_child(Fl_Type*, Fl_Type*);
  324.   void remove_child(Fl_Type*);
  325. };
  326.  
  327. extern const char scroll_type_name[];
  328. extern Fl_Menu_Item scroll_type_menu[];
  329.  
  330. class Fl_Scroll_Type : public Fl_Group_Type {
  331.   Fl_Menu_Item *subtypes() {return scroll_type_menu;}
  332. public:
  333.   virtual const char *type_name() {return scroll_type_name;}
  334.   Fl_Widget_Type *_make() {return new Fl_Scroll_Type();}
  335. };
  336.  
  337. extern const char tile_type_name[];
  338.  
  339. class Fl_Tile_Type : public Fl_Group_Type {
  340. public:
  341.   virtual const char *type_name() {return tile_type_name;}
  342.   Fl_Widget_Type *_make() {return new Fl_Tile_Type();}
  343. };
  344.  
  345. extern Fl_Menu_Item window_type_menu[];
  346.  
  347. class Fl_Window_Type : public Fl_Widget_Type {
  348.   Fl_Menu_Item* subtypes() {return window_type_menu;}
  349.  
  350.   friend class Overlay_Window;
  351.   int mx,my;        // mouse position during dragging
  352.   int x1,y1;        // initial position of selection box
  353.   int bx,by,br,bt;    // bounding box of selection
  354.   int dx,dy;
  355.   int drag;        // which parts of bbox are being moved
  356.   int numselected;    // number of children selected
  357.   enum {LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,DRAG=16,BOX=32};
  358.   void draw_overlay();
  359.   void newdx();
  360.   void newposition(Fl_Widget_Type *,int &x,int &y,int &w,int &h);
  361.   int handle(int);
  362.   virtual void setlabel(const char *);
  363.   void write_code1();
  364.   void write_code2();
  365.   Fl_Widget_Type *_make() {return 0;} // we don't call this
  366.   Fl_Widget *widget(int,int,int,int) {return 0;}
  367.   int recalc;        // set by fix_overlay()
  368.   void moveallchildren();
  369.  
  370. public:
  371.  
  372.   uchar modal, non_modal;
  373.  
  374.   Fl_Type *make();
  375.   virtual const char *type_name() {return "Fl_Window";}
  376.  
  377.   void open();
  378.  
  379.   void fix_overlay();    // update the bounding box, etc
  380.  
  381.   virtual void write_properties();
  382.   virtual void read_property(const char *);
  383.   virtual int read_fdesign(const char*, const char*);
  384.  
  385.   void add_child(Fl_Type*, Fl_Type*);
  386.   void move_child(Fl_Type*, Fl_Type*);
  387.   void remove_child(Fl_Type*);
  388.  
  389.   int is_parent() const {return 1;}
  390.   int is_group() const {return 1;}
  391.   int is_window() const {return 1;}
  392. };
  393.  
  394. extern Fl_Menu_Item menu_item_type_menu[];
  395.  
  396. class Fl_Menu_Item_Type : public Fl_Widget_Type {
  397. public:
  398.   Fl_Menu_Item* subtypes() {return menu_item_type_menu;}
  399.   const char* type_name() {return "menuitem";}
  400.   Fl_Type* make();
  401.   int is_menu_item() const {return 1;}
  402.   int is_button() const {return 1;} // this gets shortcut to work
  403.   Fl_Widget* widget(int,int,int,int) {return 0;}
  404.   Fl_Widget_Type* _make() {return 0;}
  405.   const char* menu_name(int& i);
  406.   int flags();
  407.   void write_static();
  408.   void write_item();
  409.   void write_code1();
  410.   void write_code2();
  411. };
  412.  
  413. class Fl_Submenu_Type : public Fl_Menu_Item_Type {
  414. public:
  415.   Fl_Menu_Item* subtypes() {return 0;}
  416.   const char* type_name() {return "submenu";}
  417.   int is_parent() const {return 1;}
  418.   int is_button() const {return 0;} // disable shortcut
  419.   Fl_Type* make();
  420.   // changes to submenu must propagate up so build_menu is called
  421.   // on the parent Fl_Menu_Type:
  422.   void add_child(Fl_Type*a, Fl_Type*b) {parent->add_child(a,b);}
  423.   void move_child(Fl_Type*a, Fl_Type*b) {parent->move_child(a,b);}
  424.   void remove_child(Fl_Type*a) {parent->remove_child(a);}
  425. };
  426.  
  427.  
  428. #include <FL/Fl_Menu_.H>
  429. class Fl_Menu_Type : public Fl_Widget_Type {
  430.   int textstuff(int w, Fl_Font& f, int& s, Fl_Color& c) {
  431.     Fl_Menu_ *o = (Fl_Menu_*)(w==4 ? ((Fl_Widget_Type*)this->factory)->o : this->o);
  432.     switch (w) {
  433.     case 4:
  434.     case 0: f = o->textfont(); s = o->textsize(); c = o->textcolor(); break;
  435.     case 1: o->textfont(f); break;
  436.     case 2: o->textsize(s); break;
  437.     case 3: o->textcolor(c); break;
  438.     }
  439.     return 1;
  440.   }
  441. public:
  442.   int is_menu_button() const {return 1;}
  443.   int is_parent() const {return 1;}
  444.   int menusize;
  445.   void build_menu();
  446.   Fl_Menu_Type() : Fl_Widget_Type() {menusize = 0;}
  447.   ~Fl_Menu_Type() {
  448.     if (menusize) delete[] (Fl_Menu_Item*)(((Fl_Menu_*)o)->menu());
  449.   }
  450.   void add_child(Fl_Type*, Fl_Type*) {build_menu();}
  451.   void move_child(Fl_Type*, Fl_Type*) {build_menu();}
  452.   void remove_child(Fl_Type*) {build_menu();}
  453.   Fl_Type* click_test(int x, int y);
  454.   void write_code2();
  455. };
  456.  
  457. extern Fl_Menu_Item button_type_menu[];
  458.  
  459. #include <FL/Fl_Menu_Button.H>
  460. class Fl_Menu_Button_Type : public Fl_Menu_Type {
  461.   Fl_Menu_Item *subtypes() {return button_type_menu;}
  462. public:
  463.   virtual const char *type_name() {return "Fl_Menu_Button";}
  464.   Fl_Widget *widget(int x,int y,int w,int h) {
  465.     return new Fl_Menu_Button(x,y,w,h,"menu");}
  466.   Fl_Widget_Type *_make() {return new Fl_Menu_Button_Type();}
  467. };
  468.  
  469. extern Fl_Menu_Item dummymenu[];
  470.  
  471. #include <FL/Fl_Choice.H>
  472. class Fl_Choice_Type : public Fl_Menu_Type {
  473. public:
  474.   virtual const char *type_name() {return "Fl_Choice";}
  475.   Fl_Widget *widget(int x,int y,int w,int h) {
  476.     Fl_Choice *o = new Fl_Choice(x,y,w,h,"choice:");
  477.     o->menu(dummymenu);
  478.     return o;
  479.   }
  480.   Fl_Widget_Type *_make() {return new Fl_Choice_Type();}
  481. };
  482.  
  483. #include <FL/Fl_Menu_Bar.H>
  484. class Fl_Menu_Bar_Type : public Fl_Menu_Type {
  485. public:
  486.   virtual const char *type_name() {return "Fl_Menu_Bar";}
  487.   Fl_Widget *widget(int x,int y,int w,int h) {
  488.     return new Fl_Menu_Bar(x,y,w,h);}
  489.   Fl_Widget_Type *_make() {return new Fl_Menu_Bar_Type();}
  490. };
  491. // object list operations:
  492. Fl_Widget *make_widget_browser(int x,int y,int w,int h);
  493. extern int modflag;
  494. void delete_all(int selected_only=0);
  495. void selection_changed(Fl_Type* new_current);
  496.  
  497. // file operations:
  498. void write_word(const char *);
  499. void write_string(const char *,...);
  500. int write_file(const char *, int selected_only = 0);
  501. int write_code(const char *cfile, const char *hfile);
  502.  
  503. int write_declare(const char *, ...);
  504. int is_id(char);
  505. const char* unique_id(void* o, const char*, const char*, const char*);
  506. void write_c(const char*, ...);
  507. void write_h(const char*, ...);
  508. void write_cstring(const char *);
  509. void write_cstring(const char *,int length);
  510. void write_indent(int n);
  511. void write_open(int);
  512. void write_close(int n);
  513. extern int write_number;
  514. void write_public(int state); // writes pubic:/private: as needed
  515. extern int indentation;
  516. extern const char* indent();
  517.  
  518. int read_file(const char *, int merge);
  519. const char *read_word(int wantbrace = 0);
  520. void read_error(const char *format, ...);
  521.  
  522. // check legality of c code (sort of) and return error:
  523. const char *c_check(const char *c, int type = 0);
  524.  
  525. // replace a string pointer with new value, strips leading/trailing blanks:
  526. int storestring(const char *n, const char * & p, int nostrip=0);
  527.  
  528. extern int include_H_from_C;
  529.  
  530. //
  531. // End of "$Id: Fl_Type.h,v 1.5.2.5 1999/09/10 16:40:17 bill Exp $".
  532. //
  533.